package org.ediba.siri.web.rest;

import java.beans.PropertyEditorSupport;

public class BooleanPropertyEditor extends PropertyEditorSupport {
	
	public static final String DEFAULT_TRUE = "true";
	public static final String DEFAULT_FALSE = "false";
	
	private String trueText;
	private String falseText;
	
	public BooleanPropertyEditor(){
		this.trueText = DEFAULT_TRUE;
		this.falseText = DEFAULT_FALSE;
	};
	
	public BooleanPropertyEditor(String trueText, String falseText){
		this.trueText = trueText;
		this.falseText = falseText;
	}
	
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (this.trueText.equals(text))
			this.setValue(Boolean.TRUE);
		else if (this.falseText.equals(text))
			this.setValue(Boolean.FALSE);
		else 
			this.setValue(null);
	}
	
	@Override
	public String getAsText() {
		return ((Boolean)this.getValue()).toString();
	}

}
